package cn.cdeden.aiqa.controller;

import cn.cdeden.aiqa.domain.ExpertApp;
import cn.cdeden.aiqa.domain.vo.ExpertAppVo;
import cn.cdeden.aiqa.domain.vo.UserMessageVo;
import cn.cdeden.aiqa.service.ExpertAppService;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 专家库应用接口
 */


@RestController
@RequiredArgsConstructor
@RequestMapping("/expert/apps")
public class ExpertAppController {

    private final ExpertAppService expertAppService;

    /**
     * 专家库-应用：创建应用
     */
    @PostMapping("/create")
    private R createExpertApp(@RequestBody ExpertApp expertApp) {
        expertAppService.createExpertApp(expertApp);
        return R.ok(expertApp);
    }


    /**
     * 专家库-应用：修改应用
     */
    @PutMapping("/update")
    public R<Void> updateExpertApp(@RequestBody ExpertApp expertApp) {
        expertAppService.updateExpertApp(expertApp);
        return R.ok();
    }


    /**
     * 专家库-应用：删除多个应用
     */
    @DeleteMapping("/delete")
    public R<Void> deleteExpertApp(@RequestBody List<Long> ids) {
        expertAppService.deleteExpertApps(ids);
        return R.ok();
    }


    /**
     * 专家库-应用：分页查询应用
     */
    @GetMapping("/list")
    public TableDataInfo<ExpertAppVo> getAllWikiApps(@RequestParam(required = false) String appName, @RequestParam(required = false) String wikiNames,
                                                     @RequestParam(required = false) String userName, PageQuery pageQuery) {
        return expertAppService.getAllExpertApps(appName, wikiNames, userName, pageQuery);
    }


    /**
     * 专家库-应用：增加问答次数
     */
    @PostMapping("/cr_count")
    public R<Void> crCount(
        @RequestParam Long appId){
        expertAppService.crCount(appId);
        return R.ok();
    }

    /**
     * 配置知识库对应的人员
     * @return
     */
    @PostMapping("/add/user")
    public R addUser(@RequestBody ExpertApp expertApp) {

        ExpertApp byId = expertAppService.getById(expertApp.getId());

        if (byId.getUserId() == null || byId.getUserId().equals(expertApp.getUserId())) {
            expertAppService.updateExpertApp(expertApp);
            return R.ok();
        }
        //先检查选择的用户是否已经有专家知识库了
        Long byUserId = expertAppService.getByUserId(expertApp.getUserId());
        if (byUserId >=1) {
            throw new RuntimeException("选择的用户已经关联了知识库！");
        }
        expertAppService.updateExpertApp(expertApp);
        return R.ok();
    }

    /**
     *回显用户信息
     * @param id
     * @return
     */
    @GetMapping("get/user/message/{id}")
    public UserMessageVo getUserMessage(@PathVariable("id") Long id) {
        UserMessageVo userMessage = expertAppService.getUserMessage(id);
        return userMessage;
    }

    @GetMapping("/authorized")
    public R<List<ExpertApp>> getAuthorizedApps(@RequestParam("open") int open) {
        List<ExpertApp> authorizedApps = expertAppService.getAuthorizedApps(open);
        List<ExpertApp> authorizedNewApps = new ArrayList<>();
        authorizedApps.forEach(expertApp -> {
            ExpertApp expertApp1 = new ExpertApp();
            expertApp1.setWikiIds(expertApp.getWikiIds());
            expertApp1.setUserId(expertApp.getUserId());
            expertApp1.setAppKey(expertApp.getAppKey());
            expertApp1.setAppName(expertApp.getWikiNames());
            expertApp1.setWikiNames(expertApp.getWikiNames());
            expertApp1.setWikiType(expertApp.getWikiType());
            expertApp1.setRCount(expertApp.getRCount());
            expertApp1.setOpen(expertApp.getOpen());
            expertApp1.setId(expertApp.getId());
            authorizedNewApps.add(expertApp1);
        });
        return R.ok(authorizedNewApps);
    }





}
